//index.js
//获取应用实例
const app = getApp()
//var hostName = 'http://192.168.0.100:8080';
//var hostName = 'https://www.daliandaxue.cn';

Page({
  data: {
    motto: 'Login',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    loadingHidden: false, // loading
    userInfo: {},
    swiperCurrent: 0,
    selectCurrent: 0,
    categories: [],
    activeCategoryId: 0,
    goods: [],
    scrollTop: "0",
    loadingMoreHidden: true,

    hasNoCoupons: true,
    coupons: [],
    searchInput: '',
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    banners: ""
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

  tabClick: function (e) {
    this.setData({
      activeCategoryId: e.currentTarget.id
    });
    this.getGoodsList(this.data.activeCategoryId);
  },
  //事件处理函数
  swiperchange: function (e) {
    //console.log(e.detail.current)
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  goToDetails: function (e) {
    wx.navigateTo({
      //url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
      url: "/pages/index/list/list?id=" + e.currentTarget.dataset.id
    })
  },
  tapBanner: function (e) {
    if (e.currentTarget.dataset.id != 0) {
      wx.navigateTo({
        url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
      })
    }
  },
  bindTypeTap: function (e) {
    this.setData({
      selectCurrent: e.index
    })
  },
  scroll: function (e) {
    //  console.log(e) ;
    var that = this, scrollTop = that.data.scrollTop;
    that.setData({
      scrollTop: e.detail.scrollTop
    })
    // console.log('e.detail.scrollTop:'+e.detail.scrollTop) ;
    // console.log('scrollTop:'+scrollTop)
  },
  onLoad: function () {
    var that = this;
    wx.request({
      url: app.globalData.hostName + '/microappointment/activity/getLatest',
      method: "GET",
      success: function (res) {
        var resarr = new Array();
        resarr = res.data.data;
        var arrtemp = new Array();
        
        resarr.forEach(function(item) {
          var it = {
            "url": "",
            "id": ""
          }
          it.id = item.id;
          it.pictureUrl = item.pictureList[0].pictureUrl;
          arrtemp.push(it);
        })
        that.setData({
          banners: arrtemp
        });
      }
    })
    wx.request({
      url: app.globalData.hostName + '/microappointment/activity/category',
      success: function (res) {
        // var categories = [{ id: 0, name: "全部" }];
        // if (res.data.code == 0) {
        //   for (var i = 0; i < res.data.data.length; i++) {
        //     categories.push(res.data.data[i]);
        //   }
        // }
        that.setData({
          categories: res.data.data,
          activeCategoryId: 0
        });
        that.getGoodsList(0);
      }
    })
    that.getNotice();
  },
  getGoodsList: function (categoryId) {
    if (categoryId == 0) {
      categoryId = "";
    }
    var that = this;
    wx.request({
      url: app.globalData.hostName + '/microappointment/activity/type?categoryId=' + categoryId,

      success: function (res) {
        console.log('/microappointment/activity/type?categoryId=', res);
        that.setData({
          goods: [],
          loadingMoreHidden: true
        });
        var goods = [];
        if (res.data.data.length == 0) {
          that.setData({
            loadingMoreHidden: false,
          });
          return;
        }
        for (var i = 0; i < res.data.data.length; i++) {
          goods.push(res.data.data[i]);
        }
        that.setData({
          goods: goods,
        });
      }
    })
  },

  getNotice: function () {
    
  },
  listenerSearchInput: function (e) {
    this.setData({
      searchInput: e.detail.value
    })

  },
  goToSearch: function () {
    wx.navigateTo({
      //url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
      url: "/pages/index/search/search?keyWord=" + this.data.searchInput
    })
  },
  goToDetail: function (e) {
    console.log("debug e.currentTarget.id", e.currentTarget.dataset.id);
    wx.navigateTo({
      //url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
      url: "/pages/detail/detail?id=" + e.currentTarget.dataset.id
    })
  }
})
